package Pages;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import selenium.WebDriverWrapper;

/**
 * Created by Time to be on 06.07.2014.
 */
public class TicketsPage extends BasePage {

    private static final String adultTickets = "//div[@class='adults_block passengers_wrapper']/div[@class='persons']/div[%d]";
    private static final String infantTickents = "//div[@class='infants_block passengers_wrapper']/div[@class='persons']/div[%d]";
    private static final String childerTickets = "//div[@class='children_block passengers_wrapper']/div[@class='persons']/div[%d]";
    private static final By errorPopup = By.className("error_popup");
    private static final By Button = By.id("start_search");

    public TicketsPage(WebDriverWrapper driver) {
        super(driver);
        url = "http://rozetka.com.ua/travel/IEV/MOW/";
    }

    public void buyTickets (int adults, int children, int infants) {
        WebDriverWait wait = new WebDriverWait(driver,10);
        wait.until(ExpectedConditions.elementToBeClickable(Button));

        if (adults == 0) adults++;
        driver.findElement(By.xpath(String.format(adultTickets,adults))).click();
        driver.findElement(By.xpath(String.format(infantTickents,infants))).click();
        if (children > 0) driver.findElement(By.xpath(String.format(childerTickets,children))).click();
        driver.findElement(Button).click();

    }

    public boolean isError() {
        return driver.findElements(errorPopup).size()>0;
    }
//end.
}
